package model;

import init.DBManagement;
import java.util.Vector;

import model.beans.AcuerdoBean;
import java.util.Map;

public class Agreement {
	
	private static String str_format = "yyyy/MM/dd";
	
	public static Vector list(int int_minuta, String academyCode){
		Vector arrL_list = new Vector(); 
		DBManagement db = new DBManagement();
		int int_rows;
		
		String str_query = 
			" SELECT  MIN_TECH_KEY,                           " +
			"         NUM_ACUERDO,                            " +
			"         ACUERDO,                                " +
			"         RESPONSABLE_TK,                         " +
			"         BASUSRB.NOMBRE,                         " +
			"         to_char(FECHA_ENTREGA, '" + str_format + "' ) AS FECHA_ENTREGA,  " +
			"         PRODUCTO,                               " +
			"         ULTIMA_MOD,                             " +
			"         ACU_TECH_KEY                            " +
			" FROM ACAACUR                                    " +
			"     INNER JOIN BASUSRB                          " +
			"        ON RESPONSABLE_TK = BASUSRB.USR_TECH_KEY " +
			"      WHERE MIN_TECH_KEY  = "+int_minuta+"       ";
		
		db.executeQuery(str_query);
		int_rows = db.getNumRows();
		
		AcuerdoBean bn_acuerdo=  new AcuerdoBean();
		for(int int_i=0; int_i<int_rows; int_i++){
				bn_acuerdo=  new AcuerdoBean();
				bn_acuerdo.setInt_minuta(int_minuta);
				bn_acuerdo.setInt_acuerdo( Integer.parseInt(db.getItem(int_i, 1)));
				bn_acuerdo.setStr_acuerdo(db.getItem(int_i, 2));
				bn_acuerdo.setInt_responsable(Integer.parseInt(db.getItem(int_i, 3)));
				bn_acuerdo.setStr_responsable(db.getItem(int_i, 4));
				bn_acuerdo.setStr_fecha(db.getItem(int_i, 5));
				bn_acuerdo.setStr_producto(db.getItem(int_i, 6));
				bn_acuerdo.setInt_id(Integer.parseInt(db.getItem(int_i, 8)));
				bn_acuerdo.setAcademy(academyCode);
				arrL_list.add(bn_acuerdo);
		}
		return arrL_list;
	}
	
	
	
	public static int edit(Map map_acuerdo){
		int int_edit;
		int int_id = ((Integer)map_acuerdo.get("int_id")).intValue();
		int int_minuta =  ((Integer)map_acuerdo.get("int_minuta")).intValue();
		int int_acuerdo =  ((Integer)map_acuerdo.get("int_acuerdo")).intValue();
        String str_acuerdo  = (String)map_acuerdo.get("str_acuerdo");       
        String str_fecha =  (String)map_acuerdo.get("str_fecha");
        String str_producto =  (String)map_acuerdo.get("str_producto");
        int int_responsable =  ((Integer)map_acuerdo.get("int_responsable")).intValue();
		String str_edit =
            " UPDATE ACAACUR "+
            " SET    MIN_TECH_KEY = '"+int_minuta+"', "+
            "        NUM_ACUERDO =  '"+int_acuerdo+"',"+
            "        ACUERDO = '"+str_acuerdo+"',"+
            "        RESPONSABLE_TK = '"+int_responsable+"',"+ 
            "        FECHA_ENTREGA = to_date('"+str_fecha+"', 'yyyy/MM/dd'),     " +
            "        PRODUCTO = '"+str_producto+"',"+
            "        ULTIMA_MOD = SYSDATE "+
            " where  ACU_TECH_KEY = "+int_id;
		DBManagement database = new DBManagement();
        boolean bol_insertStatus = database.executeQuery( str_edit );
        int_edit = bol_insertStatus ? 1 : 0;
        database = null;
	    return int_edit;
	}
	
	public static AcuerdoBean instance (int int_acuerdo, String academyCode){
		DBManagement db = new DBManagement();
		
		String str_query = 
			" SELECT  MIN_TECH_KEY,                                " +
			"         NUM_ACUERDO,                            " +
			"		   ACUERDO,								" +		
			"         RESPONSABLE_TK,                         " +
			"         BASUSRB.NOMBRE,                         " +
			"         to_char(FECHA_ENTREGA, '" + str_format + "' ) AS FECHA_ENTREGA,  " +
			"         PRODUCTO,                               " +
			"         ACU_TECH_KEY                            " +
			"        FROM ACAACUR                                    " +
			"     INNER JOIN BASUSRB                          " +
			"        ON RESPONSABLE_TK = BASUSRB.USR_TECH_KEY " +		
			"      WHERE  ACU_TECH_KEY = "+int_acuerdo+" "; 
		
		db.executeQuery(str_query);
		
		AcuerdoBean bn_acuerdo=  new AcuerdoBean();
		bn_acuerdo=  new AcuerdoBean();
		bn_acuerdo.setInt_minuta(Integer.parseInt(db.getItem(0,0)));
		bn_acuerdo.setInt_acuerdo( Integer.parseInt(db.getItem(0,1)));
		bn_acuerdo.setStr_acuerdo(db.getItem(0, 2));
		bn_acuerdo.setInt_responsable(Integer.parseInt(db.getItem(0, 3)));
		bn_acuerdo.setStr_responsable(db.getItem(0, 4));
		bn_acuerdo.setStr_fecha(db.getItem(0, 5));
		bn_acuerdo.setStr_producto(db.getItem(0, 6));
		bn_acuerdo.setInt_id(Integer.parseInt(db.getItem(0, 7)));
		bn_acuerdo.setAcademy(academyCode);

		return bn_acuerdo;
	}
	
	public static int add(Map map_acuerdo, int int_minuta){
		int int_add;
		//int int_acuerdo =  ((Integer)map_acuerdo.get("int_acuerdo")).intValue();
		int int_acuerdo =1;
        String str_acuerdo  = (String)map_acuerdo.get("str_acuerdo");       
        String str_fecha =  (String)map_acuerdo.get("str_fecha");
        String str_producto =  (String)map_acuerdo.get("str_producto");
        int int_responsable =  ((Integer)map_acuerdo.get("int_responsable")).intValue();
		String str_add =
            " INSERT INTO ACAACUR ( "+
            "        MIN_TECH_KEY, "+
            "        NUM_ACUERDO, "+
            "        ACUERDO, "+
            "        RESPONSABLE_TK, "+
            "        FECHA_ENTREGA, "+
            "        PRODUCTO,  "+
            "        ULTIMA_MOD "+           
            " ) VALUES ( "+
            "        '"+int_minuta+"', "+
            "        '"+int_acuerdo+"', "+
            "        '"+str_acuerdo+"', "+
            "        '"+int_responsable+"'," +           
           "          to_date( '"+str_fecha+"', 'yyyy/MM/dd'),       " +
            "        '"+str_producto +"'," +
            "        SYSDATE            "+
            " ) ";
		DBManagement database = new DBManagement();
        boolean bol_insertStatus = database.executeQuery( str_add );
        int_add = bol_insertStatus ? 1 : 0;
        database = null;
	    return int_add;
	}
	
	public static boolean delete( int acuerdo_id ){		
		String str_delete =
            " delete from ACAACUR "+
            " where  ACU_TECH_KEY = "+acuerdo_id;
		DBManagement database = new DBManagement();
        boolean bol_insertStatus = database.executeQuery( str_delete );        
        database = null;
	    return bol_insertStatus;
	}
}
